package com.nstar.base.util
{
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.display.StageDisplayState;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.SharedObject;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	
	import mx.core.Container;
	
	public class SystemUtils
	{
		//全屏模式与正常模式的切换
		//需要修改html-template目录下的index.template.html文件中的几个参数，如下
		//1.."allowScriptAccess","sameDomain" -> "allowFullScreen","true"
		//2.."allowScriptAccess","sameDomain" -> "allowFullScreen","true"
		//3..allowScriptAccess="sameDomain"	  -> "allowFullScreen"="true"
		//
		public static function toggleDisplayState(stage:Stage):void{
			stage.displayState = (stage.displayState == 
				StageDisplayState.NORMAL?StageDisplayState.FULL_SCREEN:StageDisplayState.NORMAL);
		}
		
		//当前时间与基准时间的比较结果，用于计时
		public static function calcInterval(benchmark:Date):Number{
			var now:Date = new Date();
			return now.getTime() - benchmark.getTime();
		}
		
		//保存数据到本地
		public static function setMemory(url:String,property:String,value:String):void{
			var so:SharedObject = SharedObject.getLocal(url,"/");
			so.data[property] = value;
			
			so.flush();
		}
		//从本地获取数据
		public static function getMemory(url:String, property:String):String{
			var so:SharedObject = SharedObject.getLocal(url,"/");
			return so.data[property];
		}
		
		//保存列表数据到本地
		public static function setMemoryList(url:String,propList:Array,valueList:Array):void{
			var so:SharedObject = SharedObject.getLocal(url);
			for(var i:int = 0;i<propList.length;i++){
				so.data[propList[i]] = valueList[i];
			}
			
			so.flush();
		}
		
		//取得本地列表数据
		public static function getMemoryList(url:String,propList:Array):Array{
			var ary:Array = new Array();
			var so:SharedObject = SharedObject.getLocal(url);
			for(var i:int = 0;i<propList.length;i++){
				ary.push(so.data[propList[i]]);
			}
			
			return ary;
		}
		
		//显示保存在url中的shareobject对象值
		public static function showMemory(url:String):String{
			var so:SharedObject = SharedObject.getLocal(url);
			var info:String = "";
			/* for (var item:Object in so){
				info +=item+"="+so[item]+"\n"; 
				trace(info);
			}	 */
			return info;		
		}
		
		//转到指定URL
		public static function forwardTo(url:String):void{
			navigateToURL(new URLRequest(url));
		}
		
		//产生指定范围内的随机数
		public static function randRange(min:Number, max:Number):Number {
		    var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
		    return randomNum;
		}
		
	    public static function addTab(count:int=1):String{
			var r:String="";
			for(var i:int = 0;i<count;i++){
				r += String.fromCharCode(9);
			}
			return r;
		}
		
		//保存数据到本地,dataProvider中的对象属性有 label,data
		public static function saveLocal(url:String,dataProvider:Array,
			localPath:String = null):void{
			var so:SharedObject = (localPath?SharedObject.getLocal(url,localPath):SharedObject.getLocal(url));
			for(var i:int=0;i<dataProvider.length;i++){
				var item:Object = dataProvider[i];
				so.data[item["label"]] = item["data"];
			}
			so.flush();
		}
		
		//从本地加载共享数据，dataProvider中的对象属性有 label
		public static function loadLocal(url:String,dataProvider:Array,
			localPath:String = null):Array{
			var so:SharedObject = (localPath?SharedObject.getLocal(url,localPath):SharedObject.getLocal(url));
			for(var i:int=0;i<dataProvider.length;i++){
				var item:Object = dataProvider[i];
				var value:Object = so.data[item["label"]];
				item["data"] = (value?value:"");
			}
			return dataProvider;
		}
		
		//从列表中取指定label的数据

		public static function searchItem(source:Array,label:String):String{
			for(var i:int=0;i<source.length;i++){
				if(source[i]["label"]==label){
					return source[i]["data"];
				}
			}
			return "";
		}		
		
	}
}